package com.xiaohai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaohai.projo.*;
import com.xiaohai.mapper.TeacherMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaohai.projo.Class;
import com.xiaohai.service.ClassService;
import com.xiaohai.service.CourseService;
import com.xiaohai.service.ScoreService;
import com.xiaohai.service.TeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xiaohai
 * @since 2024-01-22
 */
@Service
@Slf4j
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {
    @Autowired
    private TeacherMapper teacherMapper;


    @Autowired
    private ClassService classService;

    @Autowired
    private ScoreService scoreService;
    @Autowired
    private CourseService courseService;

    @Override
    public boolean updateInfo(Student student) {
        Teacher teacher = new Teacher();
        BeanUtils.copyProperties(student, teacher);
        log.info("12:{}", teacher);
        return teacherMapper.updateByAccount(teacher);
    }

    @Override
    public boolean updatePasswordByAccount(String account, String newPassword) {
        int update = teacherMapper.updatePasswordByAccount(account, newPassword);
        return update > 0;
    }

    @Override
    public Page<Teacher> myList(Integer currentPage, Integer pageSize, Teacher teacher) {
        Page<Teacher> page = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<Teacher> teacherLambdaQueryWrapper = new LambdaQueryWrapper<>();
//                like(teacher.getAccount() != null, Teacher::getAccount, teacher.getAccount())

        teacherLambdaQueryWrapper
                .like(teacher.getName() != null, Teacher::getName, teacher.getName())
                .eq(teacher.getGender() != null, Teacher::getGender, teacher.getGender()).like(teacher.getAge() != null
                        , Teacher::getAge, teacher.getAge())
                .like(teacher.getPhone() != null, Teacher::getPhone, teacher.getPhone());

        Page<Teacher> page1 = super.page(page, teacherLambdaQueryWrapper);
        return page1;
    }

    @Override
    public boolean add(Teacher teacher) {
        return super.save(teacher);
    }

    @Override
    public Teacher detail(String id) {
        return super.getById(id);
    }

    @Override
    public Boolean myUpdate(Teacher teacher) {
        return super.updateById(teacher);
    }

    @Override
    public boolean delete(Integer id) {
        Long a = classService.getByTeacherId(id);
        Long b = scoreService.getByTeacherId(id);
        Long c = courseService.getByTeacherId(id);
        if (a == 0 && b == 0 && c == 0) {
            return super.removeById(id);
        }
        return false;
    }

    @Override
    public Teacher getInfoByAccount(String account) {
        LambdaQueryWrapper<Teacher> teacherLambdaQueryWrapper = new LambdaQueryWrapper<>();
        teacherLambdaQueryWrapper.eq(account != null, Teacher::getAccount, account);
        Teacher one = super.getOne(teacherLambdaQueryWrapper);
        return one;
    }

    @Override
    public Integer[] getIdsByName(String teacherName) {
        LambdaQueryWrapper<Teacher> teacherLambdaQueryWrapper = new LambdaQueryWrapper<>();
        teacherLambdaQueryWrapper.like(teacherName != null, Teacher::getName, teacherName);
        List<Teacher> list = super.list(teacherLambdaQueryWrapper);
        Integer[] ids = list.stream().map(Teacher::getId).toList().toArray(new Integer[0]);
        return ids;
    }

}
